package com.example.shop.service.impl;

import com.example.shop.mapper.CommodityMapper;
import com.example.shop.service.CommodityService;
import com.example.shop.utils.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.example.shop.model.Commodity;

import java.util.List;

@Service
public class CommodityServiceImpl implements CommodityService {
    @Autowired
    private CommodityMapper commodityMapper;

    @Override
    public List<Commodity> getCommodities() {
        return commodityMapper.selectAll();
    }

    @Override
    public Commodity getCommodity(int id) {
        return commodityMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageBean<Commodity> getCommodity(int pageNo, int pageSize, String search) {
        int dataCount = commodityMapper.getDataCount();
        PageBean<Commodity> pageBean = new PageBean<>(dataCount, pageSize);
        pageBean.setPageNo(pageNo);
        int page = (pageNo - 1) * pageSize;
        if ("".equals(search) || search.length() == 0) {
            search = null;
        }
        pageBean.setDataList(commodityMapper.getCommodity(page, pageSize, search));
        return pageBean;
    }
}
